package com.tom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class Member {
        String userid;
        String name;
        String nickname;
        String pw1;
        String pw2;
        String PID;
        String sex;
        String phone;
        String email;
        String logintype;
        
        private String useridMessage;
        private String pwMessage;
        private String emailMessage;
        private String nicknameMessage;
        String loginMessage;
        private boolean login;
        String referer;
        
        //LoginServlet用
        public Member(String userid, String pw1) {
                super();
                this.userid = userid;
                this.pw1 = pw1;
        }

        public Member(){
                
        }

        
        
        

		public Member(String userid, String name, String nickname, String pw1,
				String pw2, String PID, String sex, String phone, String email,
				String logintype) {
			super();
			this.userid = userid;
			this.name = name;
			this.nickname = nickname;
			this.pw1 = pw1;
			this.pw2 = pw2;
			this.PID = PID;
			this.sex = sex;
			this.phone = phone;
			this.email = email;
			this.logintype = logintype;
		}

		

		
        public boolean login(){
                Connection conn = null;
                PreparedStatement pstmt = null;
                login = false;
                
                try {
                        DriverManager.registerDriver(new Driver());
                        conn = DriverManager.getConnection(
                                "jdbc:mysql://j.snpy.org/j102u05", "j102u05", "j102u05.989");
                        String sql = "select * from memberlist where m_userid=? and m_password=?";
                        pstmt = conn.prepareStatement(sql);
                        pstmt.setString(1, userid);
                        pstmt.setString(2, pw1);
                        ResultSet rs = pstmt.executeQuery();
                        if (rs.next()){
                                login = true;
                                nickname = rs.getString("m_nickname");
                                email = rs.getString("m_email");
                        }else{
                                loginMessage = "登入失敗";
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } finally{
                        if (conn!=null){
                                try {
                                        pstmt.close();
                                        conn.close();
                                } catch (SQLException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        }
                }
                return login;
        }
        
        public boolean validate(){
                boolean valid = true;
                if (userid.length()<4 || userid.length()>20){
                        valid = false;
                        useridMessage = "使用者帳號長度不符合";
                }
                if (nickname.length()<6 || nickname.length()>20){
                        valid = false;
                        nicknameMessage = "暱稱長度不符合";
                }
                if (!pw1.equals(pw2)){
                        valid = false;
                        pwMessage = "密碼不符合";
                }
                if (email.indexOf("@")==-1){
                        valid = false;
                        emailMessage = "電子郵件格式錯誤";
                }
                if (valid){
                        try {
                                DriverManager.registerDriver(new Driver());
                                Connection conn = DriverManager.getConnection(
                                        "jdbc:mysql://j.snpy.org/j102u05?useUnicode=true&characterEncoding=UTF-8", "j102u05", "j102u05.989");
                                
                                String sql = "INSERT INTO memberlist(m_userid, m_name,m_nickname, m_password, m_PID, m_sex, m_phone, m_email, m_logintype)"
                                		+ " values(?,?,?,?,?,?,?,?,?)";
                                PreparedStatement pstmt = conn.prepareStatement(sql);
                                pstmt.setString(1, userid);
                                pstmt.setString(2, name);
                                pstmt.setString(3, nickname);
                                pstmt.setString(4, pw1);
                                pstmt.setString(5, PID);
                                pstmt.setString(6, sex);
                                pstmt.setString(7, phone);
                                pstmt.setString(8, email);
                                pstmt.setString(9, logintype);
                                
                                int rowCount = pstmt.executeUpdate();
                        } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        
                }
                return valid;
        }
 
        
        public String getUserid() {
                return userid;
        }

        public void setUserid(String userid) {
                this.userid = userid;
        }

        public String getPw1() {
                return pw1;
        }

        public void setPw1(String pw1) {
                this.pw1 = pw1;
        }

        public String getPw2() {
                return pw2;
        }

        public void setPw2(String pw2) {
                this.pw2 = pw2;
        }

        public String getEmail() {
                return email;
        }

        public void setEmail(String email) {
                this.email = email;
        }

        public String getUseridMessage() {
                return useridMessage;
        }

        public void setUseridMessage(String useridMessage) {
                this.useridMessage = useridMessage;
        }

        public String getPwMessage() {
                return pwMessage;
        }

        public void setPwMessage(String pwMessage) {
                this.pwMessage = pwMessage;
        }

        public String getEmailMessage() {
                return emailMessage;
        }

        public void setEmailMessage(String emailMessage) {
                this.emailMessage = emailMessage;
        }

        public String getNickname() {
                return nickname;
        }

        public void setNickname(String nickname) {
                this.nickname = nickname;
        }

        public String getNicknameMessage() {
                return nicknameMessage;
        }

        public void setNicknameMessage(String nicknameMessage) {
                this.nicknameMessage = nicknameMessage;
        }

        public boolean isLogin() {
                return login;
        }

        public void setLogin(boolean login) {
                this.login = login;
        }

        public String getLoginMessage() {
                return loginMessage;
        }

        public void setLoginMessage(String loginMessage) {
                this.loginMessage = loginMessage;
        }

		public String getReferer() {
			 if (referer!=null && referer.substring(referer.lastIndexOf("/")).equals("/index.jsp")){
                 referer = null;
         }
			return referer;
		}

		public void setReferer(String referer) {
			this.referer = referer;
		}
        
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPID() {
			return PID;
		}

		public void setPID(String pID) {
			PID = pID;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}

		public String getPhone() {
			return phone;
		}

		public void setPhone(String phone) {
			this.phone = phone;
		}

		public String getLogintype() {
			return logintype;
		}

		public void setLogintype(String logintype) {
			this.logintype = logintype;
		}
        
}